/**
 * Copyright &copy; 2016-2017 <a href="https://www.lczyfz.com">联创智云</a> All rights reserved.
 */
package com.lczyfz.campusinfo.modules.campusinfohelp.web;

import com.lczyfz.campusinfo.common.persistence.Page;
import com.lczyfz.campusinfo.common.utils.ApiResultUtils;
import com.lczyfz.campusinfo.common.web.BaseController;
import com.lczyfz.campusinfo.modules.campusinfohelp.entity.CampusinfoProDetail;
import com.lczyfz.campusinfo.modules.campusinfohelp.entity.CampusinfoProblem;
import com.lczyfz.campusinfo.modules.campusinfohelp.service.CampusinfoProDetailService;
import com.lczyfz.campusinfo.modules.campusinfohelp.service.CampusinfoProblemService;
import com.lczyfz.campusinfo.modules.sys.security.SystemAuthorizingRealm;
import com.lczyfz.campusinfo.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 问题列表Controller
 * @author zms
 * @version 2018-04-16
 */
@Controller
@RequestMapping(value = "api/campusinfohelp/campusinfoProblem")
public class CampusinfoApiProblemController extends BaseController {

	@Autowired
	private CampusinfoProblemService campusinfoProblemService;
	@Autowired
	private CampusinfoProDetailService campusinfoProDetailService;
	@RequestMapping(value = "problemList")  //问题列表
	@ResponseBody
	public Map<String, Object> getproblem(CampusinfoProblem campusinfoProblem, HttpServletRequest request, HttpServletResponse response, Model model) {
		SystemAuthorizingRealm.Principal principal = UserUtils.getPrincipal();
		campusinfoProblem.setUserid(principal.toString());
		Page<CampusinfoProblem> page = campusinfoProblemService.findPage(new Page<CampusinfoProblem>(request, response), campusinfoProblem);
		Map<String, Object> map = ApiResultUtils.getBaseMapSuccess();
		map.put("page", page.getList());
		map.put("pageNo", page.getPageNo());
		map.put("count", page.getCount());
		map.put("totalPage",page.getTotalPage());
		return map;
	}
	@RequestMapping(value = "addproblem")   //我要提问
	@ResponseBody
	public Map<String, Object> ask(CampusinfoProblem campusinfoProblem, HttpServletRequest request, HttpServletResponse response, Model model) {
		SystemAuthorizingRealm.Principal principal = UserUtils.getPrincipal();
		campusinfoProblem.setUserid(principal.toString());
		campusinfoProblemService.save(campusinfoProblem);
		Map<String, Object> map = ApiResultUtils.getBaseMapSuccess();
		return map;
	}
	@RequestMapping(value = "deleteproblem")   //删除我的问题
	@ResponseBody
	public Map<String, Object> eleteproblem(CampusinfoProblem campusinfoProblem, HttpServletRequest request, HttpServletResponse response, Model model) {
		SystemAuthorizingRealm.Principal principal = UserUtils.getPrincipal();
		campusinfoProblem.setUserid(principal.toString());
		campusinfoProblemService.delete(campusinfoProblem);
		CampusinfoProDetail findcampusinfoProDetai = new CampusinfoProDetail();
		findcampusinfoProDetai.setProId(campusinfoProblem.getId());
		List<CampusinfoProDetail> campusinfoProDetails =  campusinfoProDetailService.findList(findcampusinfoProDetai);
		for(int i=0; i<campusinfoProDetails.size();i++) {
			CampusinfoProDetail campusinfoProDetail = campusinfoProDetails.get(i);
			campusinfoProDetailService.delete(campusinfoProDetail);
		}
		Map<String, Object> map = ApiResultUtils.getBaseMapSuccess();
		return map;
	}
	@RequestMapping(value = "editproblem")   //编辑我的问题
	@ResponseBody
	public Map<String, Object> editproblem(CampusinfoProblem campusinfoProblem, HttpServletRequest request, HttpServletResponse response, Model model) {
	//	SystemAuthorizingRealm.Principal principal = UserUtils.getPrincipal();
//		campusinfoProblem.setUserid(principal.toString());
		campusinfoProblemService.save(campusinfoProblem);
		Map<String, Object> map = ApiResultUtils.getBaseMapSuccess();
		return map;
	}
	@RequestMapping(value = "findById")   //问题详情
	@ResponseBody
	public Map<String, Object> findById(CampusinfoProDetail campusinfoProDetail, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<CampusinfoProDetail> page = campusinfoProDetailService.findPage(new Page<CampusinfoProDetail>(request, response), campusinfoProDetail);
		Map<String, Object> map = ApiResultUtils.getBaseMapSuccess();
		map.put("page", page);
		return map;
	}
	@RequestMapping(value = "reply")   //问题回复
	@ResponseBody
	public Map<String, Object> reply(CampusinfoProDetail campusinfoProDetail, HttpServletRequest request, HttpServletResponse response, Model model) {
		campusinfoProDetailService.save(campusinfoProDetail);
		Map<String, Object> map = ApiResultUtils.getBaseMapSuccess();
		return map;
	}
}